/**   
 * Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * 功能描述：
 * @Package: com.tce.tsfcluster 
 * @author: Yannis   
 * @date: 2019年9月2日 下午5:52:22 
 */
package com.tce.tsfcluster;

import java.util.List;
import java.util.TreeMap;

import org.apache.http.NameValuePair;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.http.BasicClient;
import com.http.HttpResponse;
import com.tce.TceUtils;
import com.tce.tke.DescribeClustersResponse;

public class TsfClient extends BasicClient {
	
	private final static String TSF_URL = "tsf.api3.tce.piccit.com.cn";
	
	public static CreateClusterResponse createCluster(CreateClusterRequest request, String region) throws Exception {
		JSONObject body = (JSONObject) JSONObject.toJSON(request);
		String actionName = "CreateCluster";
		
		TreeMap<String, Object> paramTree = TceUtils.fillTsfParam(body, actionName, region, TSF_URL, "POST", "2018-03-26");
		try {
			TreeMap<String, String> headers = TceUtils.fillHttpHead(TSF_URL);
			List<NameValuePair> bodyList = TceUtils.getParamListForBody(paramTree);
			HttpResponse resp = new TsfClient().post("https://" + TSF_URL, headers, bodyList);
			if(resp.getStatusCode() == 200) {
				if(resp.getBody().indexOf("Error") >= 0) {
					throw new Exception("create tsf cluster Failed."+resp.getBody());
				}
				JSONObject bodyJson = JSONObject.parseObject(resp.getBody());
				CreateClusterResponse response = bodyJson.getJSONObject("Response").toJavaObject(CreateClusterResponse.class);
				if(!response.getResult()) {
					throw new Exception("create tsf cluster Failed."+resp.getBody());
				}
				return response;
			}else {
				throw new Exception("create tsf cluster Failed."+resp.toString());
			}
		} catch (Exception e) {
			throw e;
		}
	}
	
	// 删除集群时的 返回值 与创建时的一致 直接复用
	public static CreateClusterResponse deleteCluster(String clusterId, String region, String reqUrl) throws Exception {
		
		JSONObject body = new JSONObject();
		body.put("ClusterId", clusterId);
		String actionName = "DeleteCluster";
		
		TreeMap<String, Object> paramTree = TceUtils.fillTsfParam(body, actionName, region, reqUrl, "DELETE", "2018-03-26");
		try {
			TreeMap<String, String> headers = TceUtils.fillHttpHead(reqUrl);
	        
			HttpResponse resp = new TsfClient().post("https://" + reqUrl, headers, TceUtils.getParamListForBody(paramTree));
			if(resp.getStatusCode() == 200) {
				JSONObject bodyJson = JSONObject.parseObject(resp.getBody());
				return bodyJson.getJSONObject("Response").toJavaObject(CreateClusterResponse.class);
			}else {
				throw new Exception("create tsf cluster Failed."+resp.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}
	
	public static AddInstancesResponse addInstancesToCluster(AddInstancesRequest request, String region) throws Exception {
		JSONObject body = (JSONObject) JSONObject.toJSON(request);

		String actionName = "AddInstance";
		JSONArray instanceArr = (JSONArray) body.remove("InstanceIdList"); 
		for(int i = 0, j=instanceArr.size(); i<j; i++) {
			body.put("InstanceIdList." + i, instanceArr.get(i));
		}
		TreeMap<String, Object> paramTree = TceUtils.fillTsfParam(body, actionName, region, TSF_URL, "POST", "2018-03-26");
		
		try {
			TreeMap<String, String> headers = TceUtils.fillHttpHead(TSF_URL);
	        
			HttpResponse resp = new TsfClient().post("https://" + TSF_URL, headers, TceUtils.getParamListForBody(paramTree));
			if(resp.getStatusCode() == 200) {
				JSONObject bodyJson = JSONObject.parseObject(resp.getBody());
				return bodyJson.getJSONObject("Response").toJavaObject(AddInstancesResponse.class);
			}else {
				throw new Exception("create tsf cluster Failed."+resp.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}

	public TsfClient() {
		super();
	}

	
	public static DescribeClustersResponse getClusters(String clusterName, String region, String tkeUrl) throws Exception {
		
		JSONObject body = new JSONObject();
		body.put("clusterName", clusterName);
		
		String actionName = "DescribeCluster";
		
		TreeMap<String, Object> paramTree = TceUtils.fillTsfParam(body, actionName, region, tkeUrl, "GET", null);
		try {
			TreeMap<String, String> headers = TceUtils.fillHttpHead(tkeUrl);
			String paramStr = TceUtils.getParamForBody(paramTree);
			System.out.println("get url-------->>>>" + "https://" + tkeUrl+"?"+paramStr);
			HttpResponse resp = new TsfClient().get("https://" + tkeUrl+"?"+paramStr, headers);
			if(resp.getStatusCode() == 200) {
				if(resp.getBody().indexOf("Error") >= 0) {
					throw new Exception("Describe Clusters Failed."+resp.getBody());
				}
				JSONObject bodyJson = JSONObject.parseObject(resp.getBody());
				if(bodyJson.getInteger("code") != 0) {
					throw new Exception("Describe Clusters Failed." + bodyJson);
				}
				return bodyJson.getJSONObject("data").toJavaObject(DescribeClustersResponse.class);
			}else {
				throw new Exception("Describe Clusters Failed."+resp.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
	}
	
}
